﻿using AnQinFrameWorkAPI.Common.Enum;
using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Blog.DTO.BlogComment;
using AnQinFrameWorkAPI.Entity.Blog.Entity;
using AnQinFrameWorkAPI.Entity.Blog.View.BlogComment;
using AutoMapper;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.Blog;

public class BlogCommentService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //构造注入
    public BlogCommentService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #region 获取博客评论列表

    public async Task<RequestResponseModel> GetBlogCommentList(BlogCommentSearchDTO blogCommentSearchDTO)
    {
        //sql
        var sql = @"
                SELECT
                    Id,
                    Code,
                    Name,
                    SuperiorId,
                    SuperiorName,
                    Order,
                    IsDelete,
                    CreateTime
                FROM
                    Department
                WHERE
                    IsDelete = 0
            ";

        //筛选
        var query = _context.Database.SqlQueryRaw<BlogCommentView>(sql)
                .AsQueryable()
                .WhereIF(a => a.BlogTextId == blogCommentSearchDTO.Id.ToString(), blogCommentSearchDTO.Id != null)
                .WhereIF(a => a.UserId == blogCommentSearchDTO.UserId.ToString(), blogCommentSearchDTO.UserId != null)
                .WhereIF(a => a.Code.Contains(blogCommentSearchDTO.Code),
                    !string.IsNullOrEmpty(blogCommentSearchDTO.Code))
                .WhereIF(a => a.Content.Contains(blogCommentSearchDTO.Content),
                    !string.IsNullOrEmpty(blogCommentSearchDTO.Content))
                .WhereIF(a => a.CreateTime > blogCommentSearchDTO.StartTime, blogCommentSearchDTO.StartTime != null)
                .WhereIF(a => a.CreateTime < blogCommentSearchDTO.EndTime, blogCommentSearchDTO.EndTime != null)
                .WhereIF(a => a.Status == blogCommentSearchDTO.Status, blogCommentSearchDTO.Status != null)
            ;

        //返回列表
        var blogCommentList = await query
            .OrderBy(a => a.CreateTime)
            .Page(blogCommentSearchDTO.PageIndex, blogCommentSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<BlogCommentView>(blogCommentList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增博客评论

    public async Task<RequestResponseModel> AddBlogComment(BlogCommentAddDTO blogCommentAddDTO)
    {
        //生成新数据
        var blogComment = _mapper.Map<BlogCommentModel>(blogCommentAddDTO);

        blogComment.Id = Guid.NewGuid();
        blogComment.CreateTime = DateTime.Now;

        //新增数据
        _context.BlogComment.Add(blogComment);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(blogComment.Id, "新增成功");
    }

    #endregion

    #region 更新博客评论

    public async Task<RequestResponseModel> PutBlogComment(BlogCommentUpdateDTO blogCommentUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.BlogComment, blogCommentUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("博客评论Id无效");
        ;

        //编辑数据
        var blogComment = _mapper.Map(blogCommentUpdateDTO, query);

        //更新表
        _context.BlogComment.Update(blogComment);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(blogCommentUpdateDTO.Id);
    }

    #endregion

    #region 删除博客评论

    public async Task<RequestResponseModel> DeleteBlogComment(SingleIdDTO deleteDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.BlogComment, deleteDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("博客评论Id无效");
        ;

        //删除操作
        _context.BlogComment.Remove(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion

    #region 博客评论-审核通过

    public async Task<RequestResponseModel> PassBlogComment(IdArrayDTO idArrayDTO)
    {
        if (idArrayDTO == null || idArrayDTO.Ids == null || idArrayDTO.Ids.Count == 0)
            return RequestResponse.ErrorRequest("博客评论IDs列表无效");
        //查询列表
        var textList = await _context.BlogComment
            .Where(a => idArrayDTO.Ids.Contains(a.Id.ToString()))
            .Where(a => a.Status == BlogEnum.ExamStatusEnum.待审核)
            .ToListAsync();
        //判定查询结果和传入id数量是否一致
        if (textList.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("博客评论IDs列表中有ID是无效的或者不是待审核状态");
        //数量结果一致，开始调整状态
        foreach (var text in textList) text.Status = BlogEnum.ExamStatusEnum.审核通过;
        //保存数据库并返回成功
        _context.BlogComment.UpdateRange(textList);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO, "审核通过成功");
    }

    #endregion

    #region 博客评论-审核不通过

    public async Task<RequestResponseModel> UnPassBlogComment(IdArrayDTO idArrayDTO)
    {
        if (idArrayDTO == null || idArrayDTO.Ids == null || idArrayDTO.Ids.Count == 0)
            return RequestResponse.ErrorRequest("博客评论IDs列表无效");
        //查询列表
        var textList = await _context.BlogComment
            .Where(a => idArrayDTO.Ids.Contains(a.Id.ToString()))
            .Where(a => a.Status == BlogEnum.ExamStatusEnum.待审核)
            .ToListAsync();
        //判定查询结果和传入id数量是否一致
        if (textList.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("博客评论IDs列表中有ID是无效的或者不是待审核状态");
        //数量结果一致，开始调整状态
        foreach (var text in textList) text.Status = BlogEnum.ExamStatusEnum.审核不通过;
        //保存数据库并返回成功
        _context.BlogComment.UpdateRange(textList);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO, "审核不通过成功");
    }

    #endregion
}